package com.biz.primus.management.geo.controller;

import com.biz.primus.base.vo.PageVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.geo.feign.GeoFeignClient;
import com.biz.primus.model.geo.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * GeoController
 *
 * @author JKLiues
 * @date 2017年10月28日
 */
@Controller
@RequestMapping("geo")
@Secured("ROLE_GEO")
public class GeoController extends AbstractBaseController {

    private final GeoFeignClient geoFeignClient;

    @Autowired
    public GeoController(@Qualifier("geoFeignClient") GeoFeignClient geoFeignClient) {
        this.geoFeignClient = geoFeignClient;
    }

    /**
     * 更新或修改省信息
     *
     * @param reqVo 更新请求Vo
     */
    @PostMapping("updateProvinceInfo")
    @PreAuthorize("hasAuthority('OPT_GEO_LIST')")
    public ModelAndView updateProvinceInfo(GeoUpdateReqVo reqVo) {
        geoFeignClient.updateProvinceInfo(reqVo);
        return this.backProvinceList(new PageVo());
    }


    @GetMapping("forwardToProvince/{provinceId}")
    @PreAuthorize("hasAuthority('OPT_GEO_EDIT')")
    public ModelAndView forwardToProvince(@PathVariable(value = "provinceId", required = false) Long provinceId) {
        ProvinceDetailRespVo province = geoFeignClient.findProvinceById(provinceId);
        return new ModelAndView("geo/edit/province")
                .addObject("province", province);
    }

    @GetMapping("addProvince")
    @PreAuthorize("hasAuthority('OPT_GEO_ADD')")
    public ModelAndView addProvince() {
        return new ModelAndView("geo/edit/province");
    }

    /**
     * 更新或修改市信息
     *
     * @param reqVo 更新请求Vo
     */
    @PostMapping("updateCityInfo")
    @PreAuthorize("hasAuthority('OPT_GEO_ADD')")
    public ModelAndView updateCityInfo(GeoUpdateReqVo reqVo) {
        geoFeignClient.updateCityInfo(reqVo);
        return this.backCitesList(reqVo.getParentId(), new CityPageListReqVo());
    }

    @GetMapping("forwardToCity/{cityId}")
    @PreAuthorize("hasAuthority('OPT_GEO_EDIT')")
    public ModelAndView forwardToCity(@PathVariable(value = "cityId", required = false) Long cityId) {
        CityDetailRespVo city = geoFeignClient.findCityById(cityId);
        return new ModelAndView("geo/edit/city")
                .addObject("city", city);
    }

    @GetMapping("{provinceId}/addCity")
    @PreAuthorize("hasAuthority('OPT_GEO_ADD')")
    public ModelAndView addCity(@PathVariable("provinceId") Long provinceId) {
        return new ModelAndView("geo/edit/city")
                .addObject("provinceId", provinceId);
    }

    /**
     * 更新或修改区信息
     *
     * @param reqVo 更新请求Vo
     */
    @PostMapping("updateDistrictInfo")
    @PreAuthorize("hasAuthority('OPT_GEO_ADD')")
    public ModelAndView updateDistrictInfo(GeoUpdateReqVo reqVo) {
        geoFeignClient.updateDistrictInfo(reqVo);
        return this.backDistrictList(reqVo.getParentId(), new DistrictPageListReqVo());
    }

    @GetMapping("forwardToDistrict/{districtId}")
    @PreAuthorize("hasAuthority('OPT_GEO_EDIT')")
    public ModelAndView forwardToDistrict(@PathVariable(value = "districtId", required = false) Long districtId) {
        DistrictDetailRespVo district = geoFeignClient.findDistrictById(districtId);
        return new ModelAndView("geo/edit/district")
                .addObject("district", district);
    }

    @GetMapping("{provinceId}/{cityId}/addDistrict")
    @PreAuthorize("hasAuthority('OPT_GEO_ADD')")
    public ModelAndView addDistrict(@PathVariable("provinceId") Long provinceId, @PathVariable("cityId") Long districtId) {
        return new ModelAndView("geo/edit/district")
                .addObject("provinceId", provinceId)
                .addObject("cityId", districtId);
    }

    /**
     * 显示数据库中全部的省信息
     *
     * @param reqVo 省分页查询请求VO
     */
    @GetMapping("provinces")
    @PreAuthorize("hasAuthority('OPT_GEO_LIST')")
    public ModelAndView backProvinceList(PageVo reqVo) {
        SpringPageVO<ProvinceListRespVo> springPage = geoFeignClient.backProvinceList(reqVo);
        return new ModelAndView("/geo/list/provinceList")
                .addObject("springPage", springPage);
    }

    /**
     * 显示数据库中市信息
     *
     * @param reqVo      市分页查询请求Vo
     * @param provinceId 省ID
     */
    @GetMapping("{provinceId}/cities")
    @PreAuthorize("hasAuthority('OPT_GEO_LIST')")
    public ModelAndView backCitesList(@PathVariable("provinceId") Long provinceId, CityPageListReqVo reqVo) {
        reqVo.setProvinceId(provinceId);
        SpringPageVO<CityListRespVo> springPage = geoFeignClient.backCitesList(reqVo);
        return new ModelAndView("geo/list/cityList")
                .addObject("springPage", springPage);
    }

    /**
     * 显示数据库中区信息
     *
     * @param reqVo  区县分页查询请求VO
     * @param cityId 城市ID
     */
    @GetMapping("{cityId}/districts")
    @PreAuthorize("hasAuthority('OPT_GEO_LIST')")
    public ModelAndView backDistrictList(@PathVariable("cityId") Long cityId, DistrictPageListReqVo reqVo) {
        reqVo.setCityId(cityId);
        SpringPageVO<DistrictListRespVo> springPage = geoFeignClient.backDistrictList(reqVo);
        return new ModelAndView("geo/list/districtList")
                .addObject("springPage", springPage);
    }

}